home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Codigo / Control del ratón / EvenBetterBlockOut / EvenBetterBlockOut.cs < prev    next >
Encoding:
Text File  |  2002-04-18  |  3.2 KB  |  104 lines

  1. //----------------------------------------------
  2. // EvenBetterBlockOut ⌐ 2001 by Charles Petzold
  3. //----------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class EvenBetterBlockOut: Form, ICaptureLossNotify
  9. {
  10.      bool      bBlocking, bValidBox;
  11.      Point     ptBeg, ptEnd;
  12.      Rectangle rectBox;
  13.  
  14.      public static void Main()
  15.      {
  16.           Application.Run(new EvenBetterBlockOut());
  17.      }
  18.      public EvenBetterBlockOut()
  19.      {
  20.           Text = "Rectßngulo mejorado II";
  21.           BackColor = SystemColors.Window;
  22.           ForeColor = SystemColors.WindowText;
  23.  
  24.                // Fijar el objeto NativeWindow
  25.  
  26.           CaptureLossNotifyWindow win = new CaptureLossNotifyWindow();
  27.           win.control = this;
  28.           win.AssignHandle(Handle);
  29.      }
  30.      protected override void OnMouseDown(MouseEventArgs mea)
  31.      {
  32.           if (mea.Button == MouseButtons.Left)
  33.           {
  34.                ptBeg = ptEnd = new Point(mea.X, mea.Y);
  35.  
  36.                Graphics grfx = CreateGraphics();
  37.                grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
  38.                grfx.Dispose();
  39.  
  40.                bBlocking = true;
  41.           }
  42.      }
  43.      protected override void OnMouseMove(MouseEventArgs mea)
  44.      {
  45.           if (bBlocking)
  46.           {
  47.                Graphics grfx = CreateGraphics();
  48.                grfx.DrawRectangle(new Pen(BackColor), Rect(ptBeg, ptEnd));
  49.                ptEnd = new Point(mea.X, mea.Y);
  50.                grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
  51.                grfx.Dispose();
  52.                Invalidate();
  53.           }
  54.      }
  55.      protected override void OnMouseUp(MouseEventArgs mea)
  56.      {
  57.           if (bBlocking)
  58.           {
  59.                Graphics grfx = CreateGraphics();
  60.                rectBox = Rect(ptBeg, new Point(mea.X, mea.Y));
  61.                grfx.DrawRectangle(new Pen(BackColor), rectBox);
  62.                grfx.Dispose();
  63.  
  64.                bBlocking = false;
  65.                bValidBox = true;
  66.                Invalidate();
  67.           }
  68.      }
  69.      protected override void OnKeyPress(KeyPressEventArgs kpea)
  70.      {
  71.           if (kpea.KeyChar == '\x001B')
  72.                Capture = false;
  73.      }
  74.      public void OnLostCapture()
  75.      {
  76.           if (bBlocking)
  77.           {
  78.                Graphics grfx = CreateGraphics();
  79.                grfx.DrawRectangle(new Pen(BackColor), Rect(ptBeg, ptEnd));
  80.                grfx.Dispose();
  81.  
  82.                bBlocking = false;
  83.                Invalidate();
  84.           }
  85.      }
  86.      protected override void OnPaint(PaintEventArgs pea)
  87.      {
  88.           Graphics grfx = pea.Graphics;
  89.  
  90.           if (bValidBox)
  91.                grfx.FillRectangle(new SolidBrush(ForeColor), rectBox);
  92.  
  93.           if (bBlocking)
  94.                grfx.DrawRectangle(new Pen(ForeColor), Rect(ptBeg, ptEnd));
  95.      }
  96.      Rectangle Rect(Point ptBeg, Point ptEnd)
  97.      {
  98.           return new Rectangle(Math.Min(ptBeg.X, ptEnd.X),
  99.                                Math.Min(ptBeg.Y, ptEnd.Y),
  100.                                Math.Abs(ptEnd.X - ptBeg.X),
  101.                                Math.Abs(ptEnd.Y - ptBeg.Y));
  102.      }
  103. }
  104.